package com.hdu.suixianglu;

import java.util.Arrays;

public class lc1356 {
    class Solution {
        public int[] sortByBits(int[] arr) {
            return Arrays.stream(arr)
                    .boxed()
                    .sorted((a, b) -> {
                        int count1 = bitOneCount(a);
                        int count2 = bitOneCount(b);
                        if (count1 == count2)
                            return a - b;
                        else
                            return count1 - count2;
                    })
                    .mapToInt(Integer::valueOf)
                    .toArray();
        }

        int bitOneCount(int num) {
            int ans = 0;
            while (num != 0) {
                ans++;
                num = num & (num - 1);
            }
            return ans;
        }
    }
}
